<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'imprest.add'"></el-button>
                <!-- <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="batch_del" v-auth="'custom.delete'"></el-button> -->
                <!-- <sc-file-import :apiObj="$API.order.entrust.orderUp" @success="success" ref="importBox" templateUrl="https://2156.mzth.cn/import.xlsx"></sc-file-import> -->
            </div>
            <div class="right-panel">
                <div class="right-panel-search" style="flex-wrap:wrap;">
                    <el-date-picker v-model="search.apply_time" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 230px" />
                    <el-button type="primary" icon="el-icon-search" @click="upsearch">搜索</el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh">重置</el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" remoteSort remoteFilter border>
                <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                <el-table-column label="申请单号" prop="reserve_code" align="center">
                    <template #default="scope">
                        <el-link type="primary" style="font-size:12px" @click="table_reserve(scope.row)">{{ scope.row.reserve_code }}</el-link>
                    </template>
                </el-table-column>
                <el-table-column label="项目编号" prop="project.project_code" align="center"></el-table-column>
                <el-table-column label="会议名称" prop="project.metting" align="center"></el-table-column>
                <!-- <el-table-column label="申请事由" prop="apply_reason" align="center"></el-table-column> -->
                <el-table-column label="申请总额" prop="apply_money" align="center"></el-table-column>
                <el-table-column label="已核销金额" prop="verification_money" align="center"></el-table-column>
                <el-table-column label="未核销金额" prop="not_verification_money" align="center"></el-table-column>
                <el-table-column label="核销凭证" prop="verification_voucher" align="center">
                    <template #default="scope">
                        <el-button type="text" size="small" @click="showFile(scope.row)" v-if="scope.row.verification_voucher">查看附件</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="申请人" prop="admin.name" align="center"></el-table-column>
                <el-table-column label="申请时间" prop="apply_time" align="center"></el-table-column>
                <el-table-column label="申请状态" prop="apply_status" align="center">
                    <template #default="scope">
                        <el-tag v-if="scope.row.apply_status == 0" type="danger">未通过</el-tag>
                        <el-tag v-if="scope.row.apply_status == 1" type="primary">申请通过</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="核销状态" prop="verification_status" align="center">
                    <template #default="scope">
                        <el-tag v-if="scope.row.verification_status == 0" type="danger">未核销</el-tag>
                        <el-tag v-if="scope.row.verification_status == 1" type="primary">核销完成</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="210">
                    <template #default="scope">
                        <el-button-group>
                            <el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'imprest.edit'" v-if="scope.row.apply_status == 0">编辑</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button text type="danger" size="small" v-auth="'imprest.delete'" v-if="scope.row.apply_status == 0">删除</el-button>
                                </template>
                            </el-popconfirm>
                            <!-- <el-popconfirm title="确定审核吗？" @confirm="table_check(scope.row)">
                                <template #reference>
                                    <el-button text type="success" size="small" v-auth="'imprest.check'" v-if="scope.row.apply_status == 0">审核</el-button>
                                </template>
                            </el-popconfirm> -->
                            <el-button text type="primary" size="small" @click="table_apply_export(scope.row, scope.$index)" v-auth="'imprest.apply'">申请单导出</el-button>
                            <el-button text type="warning" size="small" @click="table_back_export(scope.row, scope.$index)" v-if="scope.row.verification_status == 1" v-auth="'imprest.back'">返还单导出</el-button>
                            <el-button text type="success" size="small" @click="table_upload(scope.row, scope.$index)" v-if="scope.row.verification_status == 0" v-auth="'imprest.upload'">核销凭证上传</el-button>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></save-dialog>
    <load-save v-if="dialog.load" ref="loadDialog" @success="handleSuccess" @closed="dialog.load = false" draggable></load-save>
    <verification v-if="dialog.verifica" ref="verificaDialog" @success="handleSuccess" @closed="dialog.verifica = false" draggable :query="{reserve_fund_id:reserve_fund_id}"></verification>
    <imprest-file v-if="dialog.file" ref="fileDialog" @success="handleSuccess" @closed="dialog.file = false" draggable></imprest-file>
</template>

<script>
import ImprestFile from './imprestFile.vue'
import LoadSave from './loadSave.vue'
import saveDialog from './save'
import Verification from './verification.vue'

export default {
	name: '客户管理',
	components: {
		saveDialog,LoadSave,Verification,ImprestFile
	},
	data() {
		return {
			dialog: {
				save: false,
				load: false,
				verifica: false,
                file: false
			},
			apiObj: this.$API.project.Imprest.list,
			selection: [],
			search: {
				apply_time: null,
			},
            reserve_fund_id:"",
		}
	},
	mounted() {
	},
	methods: {
        success(res){
			if(res.code==1){
				this.$refs.table.refresh()
				this.$refs.importBox.close()
				this.$message.success("导入成功")
			} else{
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
        table_reserve(row) {
            this.reserve_fund_id = row.id
			this.dialog.verifica = true
			this.$nextTick(() => {
				this.$refs.verificaDialog.open('edit').setData(row)
			})
		},
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
        // 核销凭证上传
        table_upload(row) {
			this.dialog.load = true
			this.$nextTick(() => {
				this.$refs.loadDialog.open('edit').setData(row)
			})
		},
        showFile(row){
            this.dialog.file = true
			this.$nextTick(() => {
				this.$refs.fileDialog.open('show').setData(row)
			})
        },
		//查看
		table_show(row) {
            this.client_id = row.id
			this.dialog.details = true
			this.$nextTick(() => {
				this.$refs.detailsDialog.open('show').setData(row)
			})
		},
        // 审核
        async table_check(row) {
			var reqData = {id: row.id,apply_status:1}
			var res = await this.$API.project.Imprest.check.post(reqData);
			if (res.code == 1) {
                this.$refs.table.refresh()
				this.$message.success("操作成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//删除
		async table_del(row, index) {
			var reqData = {id: row.id}
			var res = await this.$API.project.Imprest.delete.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.tableData.splice(index, 1);
                this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async table_del_all(delete_ids) {
			var reqData = {id: delete_ids}
			var res = await this.$API.project.Imprest.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
        refresh(){
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
        tabChange(e){
            this.groupId = e
        },
        // 申请单导出
        async table_apply_export(row){
            var reqData = {id: row.id}
			var res = await this.$API.project.Imprest.export.post(reqData);
			if (res.code == 1) {
				const fileUrl = res.data.url;
                const link = document.createElement("a");
                link.href = fileUrl;
                link.download = fileUrl;
                link.click();
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
        },
	}
}
</script>

<style>
.titleBox {
	font-size: 14px;
	font-weight: 500;
	color: #0099ff;
	margin-left: 30px;
}
</style>
